/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function loadAccountDetailInfo()
{
    var xmlhttp;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var response = xmlhttp.responseText;
            var info = response.split(";");
            document.getElementById("infor-fullname").value=info[0];
            document.getElementById("infor-username").value=info[1];
            document.getElementById("infor-phone").value=info[2];
            document.getElementById("infor-address").value=info[3];
        //            var accountDetail ="<div id='label-information'><label>Fullname</label><br/><br/>"
        //            +"<label>Username</label><br/><br/><label>Address</label><br/><br/>"
        //            +"<label>Phone</label><br/><br/></div>"
        //            +"<div id='content-information'>"
        //            +"<input id='infor-fullname' type='text' name='txtfullname' readonly value='"+info[0]+"'/>"
        //            +"<input id='infor-username' type='text' name='txtusername' readonly value='"+info[1]+"'/>"
        //            +"<input id='infor-address' type='text' name='txtaddress' value='"+info[3]+"'/>"
        //            +"<input id='infor-phone' type='text' name='txtphone' value='"+info[2]+"' placeholder=' Số điện thoại phải từ 10 đến 12 kí số'/> "
        //            +"<font id='error-infor-phone' style='color: red; font-weight: bold;'></font></div>";
        //            document.getElementById("information").innerHTML=accountDetail;
        }
    }
    xmlhttp.open("GET","Controller?btnAction=loadAccountDetail",true);
    xmlhttp.send();
}


function accountDetailInfo()
{
    var xmlhttp;
    var AjaxPhone = document.getElementById("infor-phone").value;
    var AjaxAddress = document.getElementById("infor-address").value;
    
        
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var response = xmlhttp.responseText;
            var info = response.split(";");
            document.getElementById("infor-fullname").value=info[0];
            document.getElementById("infor-username").value=info[1];
            document.getElementById("infor-phone").value=info[2];
            document.getElementById("infor-address").value=info[3];
        }
    }
    xmlhttp.open("GET","Controller?btnAction=accountDetailInfoAjax&txtPhone="+AjaxPhone+"&txtAddress="+AjaxAddress,true);
    xmlhttp.send();
}


function changePassword()
{
    var xmlhttp;
    var AjaxOldPass = document.getElementById("pass-oldPass").value;
    var AjaxNewPass = document.getElementById("pass-newPass").value;


    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var response = xmlhttp.responseText;
            var notify = response.split(";");
            if(notify[1] == "error"){
                document.getElementById("notify-change-pass").innerHTML = "<div style='color: red; font-weight: bold;'>"+notify[0]+"</div>";
                document.getElementById("pass-oldPass").value = "";
                document.getElementById("pass-newPass").value = "";
                document.getElementById("pass-ConfirmPass").value = "";
            }else{
                document.getElementById("notify-change-pass").innerHTML = "<div style='color: blue; font-weight: bold;'>"+notify[0]+"</div>";
                document.getElementById("pass-oldPass").value = "";
                document.getElementById("pass-newPass").value = "";
                document.getElementById("pass-ConfirmPass").value = "";
            }            
        }
    }
    xmlhttp.open("GET","Controller?btnAction=changePassword&txtOldpassword="+AjaxOldPass+"&txtNewpassword="+AjaxNewPass,true);
    xmlhttp.send();
}



function validateAccountDetailChangePassword(){
    var RE_Pwd = /^.{6,10}$/;
    var oldPass = document.getElementById("pass-oldPass").value;
    var newPass = document.getElementById("pass-newPass").value;
    var confirmPass = document.getElementById("pass-ConfirmPass").value;
    var validation = true;
    if(oldPass == ""){
        document.getElementById("error-pass-oldPass").innerHTML="Vui lòng nhập mật khẩu cũ";
        validation = false;
    } else if(!RE_Pwd.test(oldPass)){
        document.getElementById("error-pass-oldPass").innerHTML="Mật khẩu từ 6 đến 10 kí tự";
        validation = false;
    }else{
        document.getElementById("error-pass-oldPass").innerHTML="";
    }
    if(newPass == ""){
        document.getElementById("error-pass-newPass").innerHTML="Vui lòng nhập mật khẩu cũ";
        validation = false;
    } else if(!RE_Pwd.test(newPass)){
        document.getElementById("error-pass-newPass").innerHTML="Mật khẩu từ 6 đến 10 kí tự";
        validation = false;
    }else{
        document.getElementById("error-pass-newPass").innerHTML="";
    }
    if(confirmPass!==newPass){
        document.getElementById("error-pass-ConfirmPass").innerHTML="Xác nhận mật khẩu không khớp";
        validation = false;
    }else{
        document.getElementById("error-pass-ConfirmPass").innerHTML="";
    }

    return validation;
}

function validateAccountDetailChangeInfo(){
    var RE_Pnum= /^\d{9}|^\d{10}|\d{11}$/;
    var validation = true;
    var phone =  document.getElementById("infor-phone").value;
    if(phone == ""){
        document.getElementById("error-infor-phone").innerHTML="Vui lòng nhập số điện thoại";
        validation = false;
    } else if(!RE_Pnum.test(phone)){
        document.getElementById("error-infor-phone").innerHTML="Số điện thoại phải từ 10 đến 12 kí số";
        validation = false;
    }else{
        document.getElementById("error-infor-phone").innerHTML="";
    }
    return validation;
}

function AccountInfo(){
    if(validateAccountDetailChangeInfo() == false){
        return false;
    }
    accountDetailInfo();
    return true;
    
}

function AccountPassword(){
    if(validateAccountDetailChangePassword() == false){
        return false;
    }
    changePassword();
    return true;
}